package com.hx.controller;

import com.hx.pojo.CollectionDemo;
import com.hx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.Map;


@Controller
public class ParamsController {

    /**
     * 集合参数
     * @param demo
     * @return
     */
    @RequestMapping(path = "/testCollection")
    public String testCollection(CollectionDemo demo){
        System.out.println(demo);
        return "success";
    }

    /**
     * 数组参数
     * @param hobbies
     * @return
     */
    @RequestMapping(path = "/testArray")
    public String testArray(String[] hobbies){
        System.out.println(hobbies == null ? "[]" : Arrays.asList(hobbies));
        return "success";
    }

    /**
     * map参数
     * @param map
     * @RequestParam 标记map参数
     * @return
     */
    @RequestMapping(path = "/testMap")
    public String testMap(@RequestParam Map<String, Object> map){
        System.out.println(map);
        return "success";
    }

    /**
     * POJO参数
     * @param user
     * @return
     */
    @RequestMapping(path = "/testPojo")
    public String testPojo(User user){
        System.out.println(user);
        return "success";
    }


    /**
     * 简单参数
     * @RequestParam : 映射请求参数
     *      value : 指定请求参数名称
     *      required : 指定请求参数是否必须，true是默认值，是必须的。false表示不是必须的
     *      defaultValue : 表示默认值。required指定成false时，如果没有这个参数，那么就使用默认值
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(path = "/testParams")
    public String testParams(@RequestParam(value = "username", required = false, defaultValue = "testName") String name, Integer age){
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return "success";
    }



}
